package com.silverhaze.diamond.client.ui.table.model;

import com.silverhaze.diamond.common.entity.CountedDetail;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * SystemUser: Ivan
 * Date: 09.07.2008
 * Time: 0:43:47
 * To change this template use File | Settings | File Templates.
 */
public class CountedDetailsTableModel extends AbstractServiceTableModel<CountedDetail> {

    private static final int colCount = 2;

    private List<CountedDetail> details;

    public CountedDetailsTableModel(){
    }

    public CountedDetailsTableModel(Set<?> details) {
        fillData(details != null ? new ArrayList(details) : new ArrayList());
    }

    public CountedDetailsTableModel(List details) {
        fillData(details);
    }

    public List<CountedDetail> getDataList() {
        return details;
    }

    public CountedDetail getObjectAtRow(int row) {
        return details.get(row);
    }

    public void fillData(List data) {
        //noinspection unchecked
        this.details = data;

        setData(new Object[data.size()][colCount]);

        Collections.sort(details, new Comparator<CountedDetail>() {
            public int compare(CountedDetail o1, CountedDetail o2) {
                return o1.getDetail().getName().compareTo(o2.getDetail().getName());
            }
        });

        int i = 0;
        for (CountedDetail detail : details) {
            getData()[i][0] = detail.getDetail().getName();
            getData()[i][1] = detail.getQuantity();

            i++;
        }

        setRowCountSize(i);
        fireTableDataChanged();
        fireTableStructureChanged();
    }

    protected void createFields() {
        setColCountSize(colCount);
        setColumnName(new String[]{
                "Деталь", "Количество"
        });
    }

    public void refreshModelAll() {
    }
}

